﻿using CPPEI.Coolzon.Designtime.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// Interaction logic for UndoPendingChanges.xaml
    /// </summary>
    public partial class UndoPendingChangesWindows : Window
    {
        private List<SolutionTreeViewNode> _nodeList;
        private List<SolutionTreeViewNode> _undoList;
        public UndoPendingChangesWindows(List<SolutionTreeViewNode> nodeList)
        {
            InitializeComponent();
            _nodeList = nodeList;
            _undoList = new List<SolutionTreeViewNode>();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (_nodeList != null && _nodeList.Count > 0)
            {
                foreach (var item in _nodeList)
                {
                    if (CanUndo(item))
                    {
                        if (!item.Caption.StartsWith("$"))
                        {
                            item.Tag = true;
                            _ctlFileListView.Items.Add(item);
                        }
                    }
                    if (item.Children.Count > 0)
                    {
                        AddChildrenNode(item.Children);
                    }
                }
            }
        }

        private void AddChildrenNode(System.Collections.ObjectModel.ObservableCollection<SolutionTreeViewNode> children)
        {
            foreach (var item in children)
            {
                if (CanUndo(item))
                {
                    if (!item.Caption.StartsWith("$"))
                    {
                        item.Tag = true;
                        _ctlFileListView.Items.Add(item);
                    }
                }
                if (item.Children.Count > 0)
                {
                    AddChildrenNode(item.Children);
                }
            }
        }

        private bool CanUndo(SolutionTreeViewNode node)
        {
            if (node.NodeStatus == NodeStatus.CheckOut || node.NodeStatus == NodeStatus.Delete)
            {
                return true;
            }
            return false;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            List<Guid> guidList = new List<Guid>();
            foreach (var item in _ctlFileListView.Items)
            {
                if ((bool)(item as SolutionTreeViewNode).Tag)
                {
                    SolutionTreeViewNode nodeItem = item as SolutionTreeViewNode;
                    guidList.Add(nodeItem.ID);
                    _undoList.Add(nodeItem);
                }
            }
            UndoThread.Start(guidList, ReportProgress, Completed);
            ProgressbarWindow.Instance.ShowProgress(false);
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        public void ReportProgress(Guid id, string name, int current, int total)
        {

        }

        public void Completed(bool HasError, int successed, int total)
        {
            ProgressbarWindow.Instance.Complete();
            this.Dispatcher.Invoke(() =>
            {
                //Designtime.Metadata.Solution solution = LocalStorage.LoadSolution(UIContext.Current.LocalWorkSpace.LocalPath, UIContext.Current.SolutionNode.ID);

                //if (_undoList.Any(p => p.NodeType == NodeType.Solution))
                //{
                //    UIContext.Current.CurrentSolution = solution;
                //}
                //else
                //{
                //    foreach (var node in _undoList)
                //    {
                //        if (node.NodeType == NodeType.Scene)
                //        {
                //            Designtime.Metadata.Scene scene = SolutionHelper.GetScene(UIContext.Current.CurrentSolution, node.ID);
                //            int index = UIContext.Current.CurrentSolution.RT_Solution.Scenes.FindIndex(p => p.Name == scene.Name);
                //            UIContext.Current.CurrentSolution.RemoveScene(scene);
                //            if (node.NodeStatus != NodeStatus.Add)
                //            {
                //                Designtime.Metadata.Scene nScene = SolutionHelper.GetScene(solution, node.ID);
                //                if (index > UIContext.Current.CurrentSolution.Scenes.Count - 1)
                //                {
                //                    UIContext.Current.CurrentSolution.Scenes.Add(nScene);
                //                    UIContext.Current.CurrentSolution.RT_Solution.Scenes.Add(nScene.RT_Scene);
                //                }
                //                else
                //                {
                //                    UIContext.Current.CurrentSolution.Scenes.Insert(index, nScene);
                //                    UIContext.Current.CurrentSolution.RT_Solution.Scenes.Insert(index, nScene.RT_Scene);
                //                }
                //            }
                //        }
                //        else if (node.NodeType == NodeType.Template)
                //        {
                //            Designtime.Metadata.Scene scene = SolutionHelper.GetScene(UIContext.Current.CurrentSolution, node.ParentID);
                //            int index = UIContext.Current.CurrentSolution.RT_Solution.Scenes.FindIndex(p => p.Name == scene.Name);
                //            UIContext.Current.CurrentSolution.RemoveScene(scene);
                //            if (node.NodeStatus != NodeStatus.Add)
                //            {
                //                Designtime.Metadata.Scene nScene = SolutionHelper.GetScene(solution, node.ParentID);
                //                if (index > UIContext.Current.CurrentSolution.Scenes.Count - 1)
                //                {
                //                    UIContext.Current.CurrentSolution.Scenes.Add(nScene);
                //                    UIContext.Current.CurrentSolution.RT_Solution.Scenes.Add(nScene.RT_Scene);
                //                }
                //                else
                //                {
                //                    UIContext.Current.CurrentSolution.Scenes.Insert(index, nScene);
                //                    UIContext.Current.CurrentSolution.RT_Solution.Scenes.Insert(index, nScene.RT_Scene);
                //                }
                //            }
                //        }
                //        else if (node.NodeType == NodeType.Resource)
                //        {
                //            Designtime.Metadata.Resource resource = SolutionHelper.GetResource(UIContext.Current.CurrentSolution, node.ID);
                //            UIContext.Current.CurrentSolution.RemoveResource(resource);

                //            if (node.NodeStatus != NodeStatus.Add)
                //            {
                //                Designtime.Metadata.Resource nResource = SolutionHelper.GetResource(solution, node.ID);
                //                UIContext.Current.CurrentSolution.AddResource(node.FolderID, node.ID, nResource.FileName, nResource.Extension);
                //            }
                //        }
                //        else if (node.NodeType == NodeType.Folder)
                //        {
                //            Designtime.Metadata.Folder folder = SolutionHelper.GetFolder(UIContext.Current.CurrentSolution, node.ID);
                //            UIContext.Current.CurrentSolution.RemoveFolder(folder);

                //            if (node.NodeStatus != NodeStatus.Add)
                //            {
                //                Designtime.Metadata.Folder nfolder = SolutionHelper.GetFolder(solution, node.ID);
                //                UIContext.Current.CurrentSolution.AddFolder(node.ParentID, node.ID, node.Name);
                //            }
                //        }
                //    }
                //}
                RefreshSolutionThread.Start();
                this.DialogResult = true;
            });
        }
    }
}
